import { useMemo } from 'react'
import _ from 'lodash'
import dayjs, { Dayjs } from 'dayjs'
import { Form, FormInstance } from 'antd'

/**
 * 响应式的 form.getFieldsValue(true)
 */
export const useFormAllValues = <T extends Record<string, any> | any[]>(
  form: FormInstance<T>,
  initialFormValues: T,
): T => {
  return (
    Form.useWatch(() => {
      const values = form.getFieldsValue(true)
      return _.isEmpty(values) ? initialFormValues : values
    }, form) || initialFormValues
  )
}

/**
 * 获取日期范围预设
 */
export const useDateRangePresets = Object.assign(
  () => {
    return useMemo(() => useDateRangePresets.getDateRangePresets(), [])
  },
  {
    /** 获取日期范围预设 */
    getDateRangePresets: () => ({
      /** 今天 */
      today: {
        label: '今天',
        value: [dayjs(), dayjs()] as [Dayjs, Dayjs],
      },
      /** 昨天 */
      yesterday: {
        label: '昨天',
        value: [dayjs().add(-1, 'day'), dayjs().add(-1, 'day')] as [Dayjs, Dayjs],
      },
      /** 本周 */
      thisWeek: {
        label: '本周',
        value: [dayjs().startOf('week'), dayjs()] as [Dayjs, Dayjs],
      },
      /** 上周 */
      lastWeek: {
        label: '上周',
        value: [dayjs().add(-1, 'week').startOf('week'), dayjs().add(-1, 'week').endOf('week')] as [Dayjs, Dayjs],
      },
      /** 本月 */
      thisMonth: {
        label: '本月',
        value: [dayjs().startOf('month'), dayjs()] as [Dayjs, Dayjs],
      },
      /** 上月 */
      lastMonth: {
        label: '上月',
        value: [dayjs().add(-1, 'month').startOf('month'), dayjs().add(-1, 'month').endOf('month')] as [Dayjs, Dayjs],
      },
      /** 最近7天 */
      last7days: {
        label: '最近7天',
        value: [dayjs().add(-6, 'day'), dayjs()] as [Dayjs, Dayjs],
      },
      /** 最近30天 */
      last30days: {
        label: '最近30天',
        value: [dayjs().add(-29, 'day'), dayjs()] as [Dayjs, Dayjs],
      },
      /** 最近60天 */
      last60days: {
        label: '最近60天',
        value: [dayjs().add(-59, 'day'), dayjs()] as [Dayjs, Dayjs],
      },
      /** 最近6个月 */
      last6months: {
        label: '最近6个月',
        value: [dayjs().add(-5, 'month').startOf('month'), dayjs()] as [Dayjs, Dayjs],
      },
    }),
  },
)
